#include "MyFigureDraw.h"

char red[] = {255, 0, 0}, green[] = {0, 255, 0}, yellow[] = {255, 255, 0};
char black[] = {0, 0, 0}, white[] = {255, 255, 255};


MyFigureDraw::MyFigureDraw(void)
{
	_graphic = NULL;
	_chart = NULL;
}

MyFigureDraw::MyFigureDraw(MyGraphics* graphic, MyChart* chart, vector<MyEffect*> effects)
{
	_graphic = graphic;
	_chart = chart;

	for (int i = 0; i < effects.size(); i++)
		_effects.push_back(effects[i]);
}

MyFigureDraw::~MyFigureDraw(void)
{
	delete _graphic;
	delete _chart;

	for (int i = 0; i < _shapes.size(); i++)
	{
		// Xoa hieu ung cu moi hinh
		if (_effects[i] != 0)
			delete _effects[i];

		// Xoa hinh
		if (_shapes[i] != 0)
			delete _shapes[i];
	}
}

void MyFigureDraw::CreateAllShape()
{
	// Tao ra 7 hinh cua 1 chart.
	_shapes.push_back(_chart->CreateStart());
	_shapes.push_back(_chart->CreateEnd());
	_shapes.push_back(_chart->CreateInput());
	_shapes.push_back(_chart->CreateOutput());
	_shapes.push_back(_chart->CreateCondition());
	_shapes.push_back(_chart->CreateProcesser());
	_shapes.push_back(_chart->CreateTransition());

	// cap nhat vi tri cac hinh
	int xFirstMove = 270;
	int yFirstMove = 15;

	for (int i = 0; i < _shapes.size(); i++)
	{
		if (i < 3)
			_shapes[i]->SetPosition(i * 100 + xFirstMove, yFirstMove);
		else
			_shapes[i]->SetPosition((i % 3) * 100 + xFirstMove, (i/3) * 100 + yFirstMove);
	}
}

void MyFigureDraw::SetGraphic(MyGraphics* graphic)
{
	_graphic = graphic;
}

void MyFigureDraw::SetChart(MyChart* chart)
{
	_chart = chart;
}

void MyFigureDraw::AddEffect(MyEffect* effect)
{
	_effects.push_back(effect);
}

void MyFigureDraw::RemoveEffect(int index)
{
	vector<MyEffect*> newEffects;

	if (0 <= index && index < _effects.size())
	{
		for (int i = 0; i < _effects.size(); i++)
		{
			if (i != index)
				newEffects.push_back(_effects[i]);
		}
		_effects = newEffects;
	}
}

void MyFigureDraw::SelectGraphic()
{
	int result;

	cout << "\nCHON THU VIEN DO HOA (Nhan phim de chon)";
	cout << "\nSo 1: GDIPlus";
	cout << "\nSo 2: CImg";
	//cout << "\nSo 3: Cairo";
	cout << endl;

	do 
	{
		cout << "\nMoi ban chon : ";
		cin >> result;
	}
	while (result <= 0 || result >= 3);

	MyGraphics* newGraphic;

	switch (result)
	{
	case 1:
		newGraphic = new MyGDIPlus();
		break;

	case 2:
		newGraphic = new MyCImg();
		break;

	//case 3:
	//	newGraphic = new MyCairo();		// Chua lam cho nay
	//	break;
	}

	if (_graphic != 0)
		delete _graphic;
	_graphic = newGraphic;
}

void MyFigureDraw::SelectChart()
{
	int result;

	cout << "\nCHON SO DO (Nhan phim de chon)";
	cout << "\nSo 1: FlowChart";
	cout << "\nSo 2: DataFlowDiagram";
	cout << "\nSo 3: ActivityDiagram";
	cout << endl;

	do 
	{
		cout << "\nMoi ban chon : ";
		cin >> result;
	}
	while (result <= 0 || result >= 4);
	
	// Khoi tao lai chart dua tren thong tin cu
	MyChart* newChart;

	switch (result)
	{
	case 1:
		newChart = new MyFlowChart();
		break;

	case 2:
		newChart = new MyDataFlowDiagram();
		break;

	case 3:
		newChart = new MyActivityDiagram();
		break;
	}

	// neu chua co chart thi khoi gan thong tin ban dau,
	// Nguoc lai thi sao chep thong tin tu chart cu
	if (_chart != 0)
	{
		ShapeInfor infor = _chart->GetShapeInfor();		// Giu lai thong tin ve chart cu

		newChart->SetShapesInformation(infor);			// Khoi gan thong tin cu cho chart moi
		delete _chart;									// Huy vung nho chart cu
	}
	else
		newChart->SetShapesInformation(0, 0, 70, 50, red, 2, yellow);

	_chart = newChart;
}


void MyFigureDraw::SelectEffect()
{
	int result;

	cout << "\nCHON HIEU UNG (Nhan phim de chon)";
	cout << "\nSo 0: Ket thuc";
	cout << "\nSo 1: Shadow";
	cout << "\nSo 2: HighLight";
	cout << "\nSo 3: Border";
	cout << "\nBan co the chon nhieu hieu ung (ket thuc chon bang so 0)";
	cout << endl;
	
	vector<int> idEffects;

	do 
	{
		cout << "\nMoi ban chon : ";
		cin >> result;
		
		// Kiem tra hieu ung da chon chua?
		if (0 < result && result < 4)
		{
			if (CheckExistEffect(idEffects, result))
				cout << "\nBan da chon hieu ung nay roi!";
		}
		else
			if (result != 0)
				cout << "\nTin hieu nay chua co. Moi ban nhap lai";
	}
	while (result != 0);

	if (idEffects.size() == 0)
		return;

	// Sap xep lai mang hieu ung
	SortIncreaseIDEffect(idEffects);

	// Tao ra tung hieu ung cho tung shape 
	MyEffect* newEffect;

	// Doan code nay can viet lai de tai su dung code (????????)
	for (int i = 0; i < idEffects.size(); i++)
	{
		switch (idEffects[i])
		{
		case 1:
			for (int i = 0; i < _shapes.size(); i++)
			{
				newEffect = new Shadow(6, 6);		// Shadow co do lech bong la (6, 6)
				newEffect->SetColor(green);			// Mau Shadow
				newEffect->SetShape(_shapes[i]);	// Ap dung shadow moi tao len shape thu i
			
				_effects.push_back(newEffect);
			}
			break;

		case 2:
			for (int i = 0; i < _shapes.size(); i++)
			{
				newEffect = new HighLight(6);		// Do day net ve HighLight
				newEffect->SetColor(red);			// Mau HighLight
				newEffect->SetShape(_shapes[i]);	// Ap dung highlight moi tao len shape thu i
			
				_effects.push_back(newEffect);
			}
			break;

		case 3:
			for (int i = 0; i < _shapes.size(); i++)
			{
				newEffect = new Border(7);			// Kich thuoc cua 8 hinh nho
				newEffect->SetColor(white);			// Mau Border
				newEffect->SetShape(_shapes[i]);	// Ap dung Border moi tao len shape thu i
			
				_effects.push_back(newEffect);
			}
			break;
		}
	}
}

void MyFigureDraw::ShowInformation()
{
	cout << "\nTHE INFORMATION OF MYFIGUREDRAW";
	cout << "\n1) THE GRAPHIC LIBRARY: " << _graphic->GetName();
	cout << "\n2) THE CHART : " << _chart->GetName();
	
	cout << "\n3) THE EFFECTS : ";

	for (int i = 0; i < _effects.size(); i++)
	{
		if ((i == 0) || (i != 0 && _effects[i]->GetName() != _effects[i - 1]->GetName()))
			cout << endl << "   + " << _effects[i]->GetName();
	}

	cout << endl;
}

void MyFigureDraw::Draw()
{
	// Ve cac hieu ung va hinh ra
	// Moi shape se co 1 cai ao (hieu ung rieng)

	// Neu co effect thi ve effect + shape. Nguoc lai thi ve shape
	if (_effects.size() > 0)
	{
		for (int i = 0; i < _effects.size(); i++)
			_effects[i]->Draw(_graphic);
	}
	else
	{
		for (int i = 0; i < _shapes.size(); i++)
			_shapes[i]->Draw(_graphic);
	}

	if (_graphic->GetName() == "CImg")
		((MyCImg*)_graphic)->Display();
}

bool MyFigureDraw::CheckExistEffect(vector<int> &idEffects, int id)
{
	// Neu mang id khong rong
	if (idEffects.size() > 0)
	{
		for (int i = 0; i < idEffects.size(); i++)
		{
			if (idEffects[i] == id)
				return true;
		}
	}

	// Nguoc lai chua ton tai + them id vao mang
	idEffects.push_back(id);

	return false;
}

void MyFigureDraw::SortIncreaseIDEffect(vector<int> &idEffects)
{
	for (int i = 0; i < idEffects.size() - 1; i++)
		for (int j = i + 1; j < idEffects.size(); j++)
		{
			if (idEffects[i] > idEffects[j])
			{
				int temp = idEffects[i];
				idEffects[i] = idEffects[j];
				idEffects[j] = temp;
			}
		}
}


